package com.durian.ui.adapter.multi;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.viewbinding.ViewBinding;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;

/**
 * 说明：ItemViewBindingProvider
 * <p/>
 * 作者：Fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2020/08/05 14:02
 * <p/>
 * 版本：version 1.0
 */
public abstract class ItemViewBindingProvider<VB extends ViewBinding,C> extends ItemViewProvider<C>{

    @NonNull
    @Override
    protected MultiItemViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        Class<VB> clazz = getViewBindingClass();
        MultiItemViewBindingHolder holder = null;
        try {
            Method method = clazz.getMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
            VB viewBinding = (VB) method.invoke(null,inflater,parent,false);
            holder = new MultiItemViewBindingHolder(viewBinding);
            holder.mInflater = inflater;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return holder;
    }

    @Override
    protected void convert(@NonNull MultiItemViewHolder holder, @NonNull C item, int position) {
        if (holder instanceof MultiItemViewBindingHolder){
            MultiItemViewBindingHolder<VB> viewBindingHolder = (MultiItemViewBindingHolder<VB>) holder;
            convert(viewBindingHolder,viewBindingHolder.viewBinding,item,position);
        }
    }

    protected abstract void convert(MultiItemViewBindingHolder<VB> holder,@NonNull VB viewBinding, @NonNull C item, int position);

    protected Class<VB> getViewBindingClass(){
        return (Class<VB>) ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    @Override
    protected View getItemView(Context context) {
        return null;
    }

    @Override
    protected int getItemLayoutId() {
        return 0;
    }
}
